package com.itheima.esdemo;

import com.alibaba.fastjson.JSON;
import com.itheima.esdemo.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//文档排序和分页
import java.io.IOException;

public class EsDemo04 {
    private RestHighLevelClient restHighLevelClient;
    //配置客户端连接地址
    @Before
    public void init(){
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        // 要连接的es地址
                        new HttpHost("192.168.200.150",9200,"http")
                        ,new HttpHost("192.168.200.151",9200,"http")   // 如果是集群的话可以写多个
                        ,new HttpHost("192.168.200.152",9200,"http")
                        ,new HttpHost("192.168.200.153",9200,"http")
                )
        );
    }


    //sort排序
    @Test
    public void documentSort() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.sort("age", SortOrder.ASC);//按年龄升序
        builder.sort("gender", SortOrder.DESC);//按性别降序
        printDocumentResult(builder);
    }

    //排序并分页
    @Test
    public void documentPage() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        int page = 1;//从第几页开始
        int pageSize = 3;//每页显示几条
        builder.from((page-1)*pageSize);//从第几条开始
        builder.size(pageSize);
        printDocumentResult(builder);
    }


    public void printDocumentResult(SearchSourceBuilder builder) throws IOException {
        // 1. 创建搜索请求   SearchRequest
        SearchRequest request = new SearchRequest("user");
        request.source(builder);
        // 2. 客户端执行搜索请求  client.search
        SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 3. 解析响应结果   SearchResponse
        // response.getHits
        SearchHits hits = searchResponse.getHits(); // 查询结果
        // .getToTalHits().value 查询结果总数量
        System.out.println("查询结果数量:" + hits.getTotalHits().value);
        // .getHits  得到文档列表
        SearchHit[] docList = hits.getHits();
        //      遍历文档列表
        for (SearchHit searchHit : docList) {
            //          .getSource 获取文档内容
            String userJson = searchHit.getSourceAsString();
            User user = JSON.parseObject(userJson,User.class);
            System.out.println(user);
        }
        //          .getHighligher 获取高亮处理结果
        // response.getAggregations 获取聚合结果
    }





    //关闭客户端
    @After
    public void close() throws IOException {
        restHighLevelClient.close();
    }

}
